package member

import (
	"fmt"
	"yunj/pkg/global"
	"yunj/pkg/yunj/app/controller/admin"
	"yunj/pkg/yunj/app/enum/stateenum"
	"yunj/pkg/yunj/app/service/admin/memberservice"
	"yunj/pkg/yunj/core/response"
	"yunj/pkg/yunj/util"

	"github.com/gin-gonic/gin"
)

type Member struct {
	admin.Controller
}

// 列表
func (ctrl *Member) List(ctx *gin.Context) {
	ctrl.RenderTable(ctx, memberservice.MemberPage.ListBuilder(ctx))
}

// 添加
func (ctrl *Member) Add(ctx *gin.Context) {
	isEdit := false
	isInfo := false
	ctrl.RenderBuilder(ctx, memberservice.MemberPage.FormBuilder(ctx, isEdit, isInfo), "member/form", map[string]any{
		"isEdit":                      isEdit,
		"departmentPostionRoleIdsApi": util.BuildAdminUrl("/member/departmentPostionRoleIds"),
	})
}

// 编辑
func (ctrl *Member) Edit(ctx *gin.Context) {
	isEdit := true
	isInfo := false
	ctrl.RenderBuilder(ctx, memberservice.MemberPage.FormBuilder(ctx, isEdit, isInfo), "member/form", map[string]any{
		"isEdit":                      isEdit,
		"departmentPostionRoleIdsApi": util.BuildAdminUrl("/member/departmentPostionRoleIds"),
	})
}

// 个人信息
func (ctrl *Member) Info(ctx *gin.Context) {
	isEdit := true
	isInfo := true
	ctrl.RenderBuilder(ctx, memberservice.MemberPage.FormBuilder(ctx, isEdit, isInfo), "member/form", map[string]any{
		"isEdit":                      isEdit,
		"departmentPostionRoleIdsApi": util.BuildAdminUrl("/member/departmentPostionRoleIds"),
	})
}

// 部门岗位下拉搜索可选项
func (ctrl *Member) DepartmentPostionDropdownSearchOptions(ctx *gin.Context) {
	options, err := memberservice.MemberPage.DepartmentPostionDropdownSearchOptions(ctx)
	if err != nil {
		response.Fail(ctx, err.Error())
	} else {
		response.Success(ctx, options)
	}
}

// 部门岗位的角色ids
func (ctrl *Member) DepartmentPostionRoleIds(ctx *gin.Context) {
	departmentPostionIdsRaw, err := util.PostParam(ctx, "department_postion_ids", []any{})
	if err != nil {
		response.Fail(ctx, fmt.Sprintf("参数异常！%v", err))
		return
	}
	departmentPostionIdsVal, err := util.ToAnySlice(departmentPostionIdsRaw)
	if err != nil {
		response.Fail(ctx, fmt.Sprintf("参数异常2！%v", err))
		return
	}
	departmentPostionIds := util.ToUint64Slice(departmentPostionIdsVal)
	if len(departmentPostionIds) == 0 {
		response.Success(ctx, map[string]any{"role_ids": []uint64{}})
		return
	}
	var roleIds []uint64
	tablePrefix := global.Config.Database.TablePrefix
	err = global.MySQL.Table(tablePrefix+"admin_department_postion dp").
		Joins("join "+tablePrefix+"admin_postion_role as pr on dp.postion_id = pr.postion_id").
		Where("dp.id in ? and state = ?", departmentPostionIdsRaw, stateenum.NORMAL).
		Pluck("role_id", &roleIds).Error
	if err != nil {
		response.Fail(ctx, fmt.Sprintf("部门岗位的角色数据查询异常！%v", err))
		return
	}
	response.Success(ctx, map[string]any{"role_ids": roleIds})
}

// 下拉搜索可选项
func (ctrl *Member) DropdownSearchOptions(ctx *gin.Context) {
	options, err := memberservice.MemberPage.DropdownSearchOptions(ctx)
	if err != nil {
		response.Fail(ctx, err.Error())
	} else {
		response.Success(ctx, options)
	}
}
